package com.cty;

import com.cty.mapper.UserMapper;
import com.cty.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;

public class test {

    public SqlSession sqlSession;

    @BeforeEach //测试方法开始之前的赋值方法
    public void before() throws IOException {
        InputStream inputStream =
                Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = build.openSession(true);
    }

    @AfterEach //测试方法执行之后的资源释放方法
    public void after() {
        sqlSession.close();
    }

    @Test
    public void test_01() throws IOException {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        user.setUsername("nihao");
        user.setPassword("123456");
        System.out.println(user.getId());
        System.out.println("------------------");
//        user.setId(1);//更新时要传入参数
//        int insert = mapper.insert(user);
//        System.out.println("result =" + insert);
//        int update = mapper.update(user);
//        System.out.println("update = " + update);
//        List<User> users = mapper.selectAll();
//        System.out.println("users = " + users);
//        int delete = mapper.delete(1);
//        System.out.println("delete = " + delete);
//        User user1 = mapper.selectById(2);
//        System.out.println("user1 = " + user1);
        int insert = mapper.insert(user);
        System.out.println(insert);
        System.out.println(user.getId());
    }
}
